import os
import time

import pytest

from common.readData import readData
from common.Assert import Assert
from common.DButils import select,back_up,rollback
from common import loginOperater
from selenium import webdriver
from common.comDB import comDB
from common.serviceWeb import ServiceWeb
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

com_db=comDB()
read_data=readData()

class TestBuy():
    @pytest.mark.parametrize("username,password,paypwd,type,expect", read_data.read_data(7, 5))
    def test_buy(self, username,password,paypwd,type,expect):
        service = ServiceWeb()

        backup = []

        try:
            # 拉起浏览器
            chromeDriver = service.get_driver("http://localhost:90/")
            # 链接数据库
            con, cursor = com_db.get_con()
            # 数据库备份
            backup1 = back_up("flow_of_funds")
            backup2=back_up("bankcard")

            # 登录
            login = loginOperater.LoginOperater()
            login.login(chromeDriver, username, password)
            time.sleep(1)

            sql1="SELECT * FROM flow_of_funds "
            result1=len(select(sql1,()))

            # 选择理财
            if type == 1:
                chromeDriver.get("http://localhost:90/user/finance/toChangeMoney.html")
                xpath="/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button"

            elif type == 2:
                chromeDriver.get("http://localhost:90/user/finance/toTermFinancial.html")
                xpath="/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[8]/button"

            elif type == 3:
                chromeDriver.get("http://localhost:90/user/finance/toFundProduct.html")
                xpath="/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button"
            # 初始化显式等待对象
            wait = WebDriverWait(chromeDriver, 10)
            el6 = wait.until(
                EC.element_to_be_clickable((By.XPATH, xpath))
            )
            el6.click()

            el7 = wait.until(
                EC.visibility_of_element_located((By.XPATH, "/html/body/div[3]/div[2]/input"))
            )
            el7.send_keys(paypwd)
            el8 = wait.until(
                EC.element_to_be_clickable((By.XPATH, "//*[@id='layui-layer1']/div[3]/a[1]"))
            )
            el8.click()
            time.sleep(1)

            result2=len(select(sql1,()))
            result=result2-result1
            # 查询结果
            result2 = len(select(sql1, ()))
            result = result2 - result1

            # 断言
            screenshot_dir = "D:\pythonproject\自动化_1封装\失败截图\buy"
            screenshot_path = f"{screenshot_dir}/{username}失败截图.png"
            Assert(chromeDriver, result, expect, screenshot_path)

        finally:
            # 数据库恢复
            rollback(backup1, "flow_of_funds")
            rollback(backup2, "bankcard")
            # 资源清理
            cursor.close()
            con.close()
            chromeDriver.quit()

            chromeDriver.quit()

